{extend name="base"/}
{block name="resources"}
<style>
    .ns-form {margin-top: 0;}
</style>
{/block}
{block name="main"}
<div  class="layui-form ns-form" id="app">
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>房型名称：</label>
        <div class="layui-input-block">
            <input type="text" name="type_name" lay-verify="required" value="{$info.type_name}" class="layui-input ns-len-long">
        </div>
    </div>


    <div class="layui-form-item" v-for="(item,index) in facilities">
        <label class="layui-form-label"><span class="required">*</span>{{item.title}}：</label>
        <div class="layui-input-block">
            <input type="text"  v-model="item.value" lay-verify="required" class="layui-input ns-len-long">
        </div>
    </div>


    <!-- 表单操作 -->
    <div class="ns-form-row">
        <button type="button" class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
    </div>

    <!-- 获取label_id -->
    <input type="hidden" name="type_id" lay-verify="required" value="{$info.type_id}"  class="layui-input">
</div>
{/block}
{block name="script"}
<script src="STATIC_JS/vue.js"></script>
<script>
    var facilities_json = {:json_encode($info.facilities_json, JSON_UNESCAPED_UNICODE)};

    var app = new Vue({
        el: "#app",
        data: {
            facilities: facilities_json
        },
    });

    layui.use('form', function(){
        var form = layui.form;
        var repeat_flag = false; //防重复
        form.render();

        form.on('submit(save)', function(data){

            data.field.facilities =  JSON.stringify(app.facilities);
            if(repeat_flag) return;
            repeat_flag = true;

            $.ajax({
                url:ns.url("hotel://hotel/roomtype/edit"),
                data: data.field,
                dataType:'JSON',
                type:'POST',
                success:function(res){
                    repeat_flag = false;

                    if (res.code == 0) {
                        layer.confirm('编辑成功', {
                            title:'操作提示',
                            btn: ['返回列表', '继续操作'],
                            yes: function(){
                                location.href = ns.url("hotel://hotel/roomtype/lists")
                            },
                            btn2: function() {
                                location.reload();
                            }
                        });
                    }else{
                        layer.msg(res.message);
                    }
                }
            });
        });

        form.verify({
            num: function (value) {
                if (value == '') {
                    return;
                }
                if (value%1 != 0) {
                    return '排序数值必须为整数';
                }
                if (value < 0) {
                    return '排序数值必须为大于0';
                }
            }
        });
    });
</script>
{/block}